
var redis = require('../db/redis')
var pu = require('../tool/public')
var mg = require('../db/userdb')
var jur_module = require('../tool/module')


class jur_class {

    /*查询全部权限*/
    get_jur_module (req, res, next) {
        pu.success('OK', jur_module, res)
    }

    /*查询当前公司下全部权限*/
    async get_team_jur_module (req, res, next) {
        let team = req.headers.team
        if (!team || team.length     != 24) {
            pu.fail('request team error', res)
        } else {

            let jur = await mg.findOne('team', { _id: mg.ObjectID(team) })
            pu.success('OK', jur.jur, res)
        }
    }
    /*查找控件权限*/
    async find_control_jur(req, res, next) {

    }
    /*新增控件权限*/
    async add_control_jur(req, res, next) {

    }


    /*新增权限*/
    async add_jur (req, res, next) {
        var module = req.body.module
        var name = req.body.name
        var team_id = req.headers.team
        let isnull = pu.isnull([module, team_id, name])
        if (isnull) {
            pu.fail('参数不正确', res)
            return
        }
        if (typeof module == 'string') {
            try {
                console.log(module)
                module = JSON.parse(module)
            } catch (e) {
                console.log(e)
                pu.fail('参数不正确', res)
                return
            }
        }
        let insert = {
            teamid: mg.ObjectID(team_id),
            name: name,
            time: +new Date(),
            module: module,
            adduserid: mg.ObjectID(req.headers.uid),
            is_delete: 0
        }
        let inserinfo = await mg.insertOne('jurinfor', insert)
        insert._id = inserinfo.insertedId
        await redis.hset('JURINFO_' + team_id, insert._id.toString(), JSON.stringify(insert.module))
        pu.success('OK', insert, res)
    }

    /*查询权限*/
    async find_jur (req, res, next) {
        var _id = req.body._id
        var is_delete = req.body.is_delete
        var team_id = req.headers.team

        var text = req.body.text

        if (_id) {//查询单个
            let info = await redis.hget('JURINFO_' + team_id, _id)
            if (info) {
                pu.success('OK', JSON.parse(info), res)
            } else {
                pu.success('OK', '', res)
            }

        } else {

            var where = { teamid: mg.ObjectID(team_id) }

            if (text) {
                where.name = { $regex: text }
            }
            if (is_delete || is_delete == 0) {
                where.is_delete = is_delete
            }
            console.log('findjurinfor', where)
            let info = await mg.find('jurinfor', where)
            console.log(JSON.stringify(info))
            pu.success('OK', info, res)

        }
    }

    /*修改权限*/
    async update_jur (req, res, next) {
        var _id = req.body._id
        var name = req.body.name
        var team_id = req.headers.team
        var module = req.body.module
        var is_delete = Number(req.body.is_delete)
        let isnull = pu.isnull([_id, is_delete])
        if (isnull) {
            pu.fail('参数不正确', res)
            return
        }
        var where = {}
        if (name) {
            where.name = name
        }
        if (module) {
            where.module = module
        }
        if (is_delete || is_delete == 0) {
            where.is_delete = Number(is_delete)

        }
        if (JSON.stringify(where) == '{}') {
            pu.fail('没有修改内容', res)
            return
        } else {
            if (is_delete == 0) {
                await redis.hset('JURINFO_' + team_id, _id, JSON.stringify(module))
            } else {
                await redis.hdel('JURINFO_' + team_id, _id)
            }
            await  mg.updateOne('jurinfor', { _id: mg.ObjectID(_id) }, { $set: where })
            pu.success('OK', '', res)
        }
    }

    /*设置权限*/
    async setting_jur (req, res, next) {

        let team_id = req.headers.team
        let team_userid = req.body.team_userid
        let role_id = req.body.role_id
        let jur_id = req.body.jur_id


        let isnull = pu.isnull([jur_id])
        if (isnull) {
            pu.fail('参数不正确', res)
            return
        }
        if (!team_userid && !role_id) {
            pu.fail('参数不正确', res)
            return
        }

        /*   1 绑定用户
         *   2 用户中设置权限id
         * */
        /*绑定用户*/
        if (team_userid && team_userid.length == 24) {

            if (jur_id) {
                await mg.updateOne('team_users', { _id: mg.ObjectID(team_userid) }, { $set: { jur_id: mg.ObjectID(jur_id) } })
                await redis.hset('JURINFO_USER_' + team_id, team_userid, jur_id)
            } else {
                await mg.updateOne('team_users', { _id: mg.ObjectID(team_userid) }, { $set: { jur_id: '' } })
                await redis.hdel('JURINFO_USER_' + team_id, team_userid)
            }
            pu.success('OK', '', res)
            return
        }
        /*
        * 绑定角色
        *
        * 1、角色添加list
        * 2、权限添加角色
        * 3、删除多个list
        * */

        if (role_id && role_id.length == 24) {

            if (jur_id) {
                if (typeof jur_id !== 'object') {
                    pu.fail('权限id必须是数组', res)
                    return
                }
                await mg.updateOne('role', { _id: mg.ObjectID(role_id) }, { $set: { jurlist: jur_id } })
                await redis.hset('JURINFO_ROLE_' + team_id, role_id, jur_id.toString())
                pu.success('OK', '', res)
            } else {
                await mg.updateOne('role', { _id: mg.ObjectID(role_id) }, { $set: [] })
                await redis.hdel('JURINFO_ROLE_' + team_id, role_id)
                pu.success('OK', '', res)
            }
            return
        } else {
            pu.fail('参数不正确', res)
            return
        }


    }

    /*删除权限*/
    async delete_jur (req, res, next) {
        var _id = req.body._id
        let team_id = req.headers.team
        let isnull = pu.isnull([_id])
        if (isnull) {
            pu.fail('参数不正确', res)
            return
        }
        let jurinfo = await mg.findOne('team_users', { team_id: mg.ObjectID(team_id), jur_id: mg.ObjectID(_id) })
        if (jurinfo) {
            pu.fail('当前权限还绑定有用户 需要先解除绑定', res)
            return
        }

        jurinfo = await mg.findOne('role', { team_id: mg.ObjectID(team_id), jurlist: { $elemMatch: { $eq: mg.ObjectID(_id) } } })
        if (jurinfo) {
            pu.fail('当前权限还绑定有角色 需要先解除绑定', res)
            return
        }
        await mg.deleteOne('jurinfor', { _id: mg.ObjectID(_id) })
        await redis.hdel('JURINFO_' + team_id, _id)
        pu.success('OK', '', res)
    }

    /*查询单个用户拥有权限*/
    async find_user_jur (req, res, next) {

        var _id = req.body._id

        if (_id) {
            req.headers.uid = _id
        }
        //获取绑定的权限
        var jurlist = []
        //获取全部角色
        let role = await redis.get('USER_ROLE_' + req.headers.uid)
        if (role) {
            role = role.split(',')
            //获取角色中全部权限
            for (var i = 0; i < role.length; i++) {
                let jurid = await redis.hget('JURINFO_ROLE_' + req.headers.team, role[i])
                if (jurid) {
                    jurid = jurid.split(',')
                    jurlist = jurlist.concat(jurid)
                }
            }
        }
        let juid = await redis.hget('JURINFO_USER_' + req.headers.team, req.headers.uid)
        if (juid) {
            jurlist.push(juid)
        }
        pu.success('OK', jurlist, res)
    }


}


module.exports = new jur_class()
